package stu.aistar.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import stu.aistar.model.pojo.Result;
import stu.aistar.model.pojo.Session;
import stu.aistar.model.pojo.User;
import stu.aistar.model.vo.SessionVo;
import stu.aistar.service.ISessionService;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/session")
public class SessionController {
    @Autowired
    private ISessionService sessionService;

    @RequestMapping("/save")
    public Result save(Session session){
        Result result;
        Integer row = sessionService.save(session);
        if (row == 1){
            result = new Result("200","插入成功",null);
        }else {
            result = new Result("500","插入失败",null);
        }
        return result;
    }

    @RequestMapping("/findAll")
    public Map<String,Object> findAll(HttpServletRequest request){
        String page = request.getParameter("page");
        String pageSize = request.getParameter("limit");
        Map<String,Object> sessionMap = new HashMap<>();
        PageInfo<SessionVo> sessions = sessionService.findSessionsByPage(page,pageSize);
        sessionMap.put("code",0);
        sessionMap.put("msg","查找成功");
        //数据总数
        sessionMap.put("count",sessions.getTotal());
        sessionMap.put("data",sessions.getList());
        return sessionMap;
    }

    @RequestMapping("/delSessionById")
    public Result delSessionById(@RequestParam("id") Integer id){
        Integer row = sessionService.deleteSession(id);
        Result result;
        if (row == 1){
            result = new Result("200","删除成功",null);
        }else {
            result = new Result("500","删除失败",null);
        }
        return result;
    }

    @RequestMapping("/editSession")
    public Result editSession(@RequestBody SessionVo sessionVo){
        System.out.println(sessionVo);
        Integer row = sessionService.updateSession(sessionVo);
        Result result;
        if (row == 1){
            result = new Result("200","修改成功",null);
        }else {
            result = new Result("500","修改失败",null);
        }
        return result;
    }
}
